﻿using System;
using System.Collections.Generic;
using Tanmia.Basis.Core;
using Tanmia.Basis.Data;
using Tanmia.Basis.Data.NHibernate;

namespace Tanmia.Basis.Domain
{
    /// <summary>
    /// Represent the base class for all domain objects of the business application.
    /// </summary>
    /// <typeparam name="TCore">BusinessObject which is going to be manipulated through this DomainObject.</typeparam>
    public abstract class BusinessDomain<TCore>
          where TCore : BaseBusinessObject
    {
        protected IRepository<TCore> Repository { get; set; }
        protected ISessionManager SessionManager { get; set; }

        public void CloseSession()
        {
            SessionManager.Close();
        }

        /// <summary>
        /// Adds or Updates business entity object against the database.
        /// </summary>
        /// <param name="entity"></param>
        /// <exception cref="System.ArgumentNullException">Throw when the parameter is null.</exception>
        public virtual void Save(TCore entity)
        {
            Repository.Save(entity);
        }

        /// <summary>
        /// Delete business entity object from the database.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">Throw when the parameter is null.</exception>
        public virtual void Delete(TCore entity)
        {
            Repository.Delete(entity);
        }

        /// <summary>
        /// Return the business entity which hold the passed entityID.
        /// </summary>
        /// <exception cref="System.ArgumentException">Throw when the parameter is 0.</exception>
        public virtual TCore Get(int entityID)
        {
            return Repository.Get(entityID);
        }

        /// <summary>
        /// Return all business entity from the database.
        /// </summary>
        /// <returns></returns>
        public virtual IList<TCore> Gets()
        {
            return Repository.Gets();
        }

        /// <exception cref="System.ArgumentException">Throw when the parameter is 0.</exception>
        public virtual IList<TCore> Gets(Specification<TCore> specs)
        {
            return Repository.Gets(specs);
        }

        public virtual IList<TCore> Gets(System.Linq.Expressions.Expression<Func<TCore, bool>> satisfy)
        {
            return Repository.Gets(satisfy);
        }
    }
}
